package com.torry.workflow.common.util;

import com.torry.workflow.common.consts.AuthConstant;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Http上下文工具类
 *
 * @author torry
 * @date 2021/11/1 16:57
 **/
public class HttpContextUtil {

    /**
     * 获取当前request上下文
     *
     * @return javax.servlet.http.HttpServletRequest
     * @author torry
     * @date 2021-11-03 15:04
     */
    public static HttpServletRequest getRequest() {

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        return requestAttributes.getRequest();
    }


    public static HttpServletResponse getResponse() {

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        return requestAttributes.getResponse();
    }

    /**
     * 获取请求来源
     *
     * @author torry
     * @date 2021/11/7 9:26
     */
    public static Integer getRequestFrom() {
        return (Integer) getRequest().getAttribute("REQUEST_FROM");
    }

    /**
     * 获取请求头中的token
     * @return
     */
    public static String getRequestToken(){
        return getRequest().getHeader(AuthConstant.TOKEN_NAME);
    }
}
